package com.amit.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


@WebServlet(name="login",urlPatterns={"/login.jlc"})
public class LoginServlet extends HttpServlet{
	
	UserService us=null;
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		
		us=new UserService();
		
		
	}
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException 
			{
		
		
		String un=request.getParameter("username");
		String pw=request.getParameter("password");
		
		int x=us.verifyUser(un, pw);
		HttpSession session=request.getSession();
		String result=" ";
		
		if(x==1)
		{
			session.setAttribute("UN", un);
			result="home.jsp";
			
		}
		else
		{
			String msg="Invalid Username or password";
			request.setAttribute("MSG", msg);
			result="index.jsp";
		}
		RequestDispatcher rd=request.getRequestDispatcher(result);
		rd.forward(request, response);
		
	}
	
	

}
